package service

import (
	"black_mamba/global"
	"black_mamba/model"
	"black_mamba/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateInventory
//@description: 创建Inventory记录
//@param: Inventory model.Inventory
//@return: err error

func CreateInventory(inventory model.Inventory) (err error) {
	err = global.BM_DB.Create(&inventory).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteInventory
//@description: 删除Inventory记录
//@param: Inventory model.Inventory
//@return: err error

func DeleteInventory(inventory model.Inventory) (err error) {
	err = global.BM_DB.Delete(&inventory).Error
	return err
}

func UpdateInventory(inventory model.Inventory) (err error) {
	err = global.BM_DB.Debug().Save(&inventory).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetInventory
//@description: 根据id获取Inventory记录
//@param: id uint
//@return: err error, Inventory model.Inventory

func GetInventory(id uint) (err error, inventory model.Inventory) {
	err = global.BM_DB.Where("id = ?", id).First(&inventory).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetInventoryInfoList
//@description: 分页获取Inventory记录
//@param: info request.InventorySearch
//@return: err error, list interface{}, total int64

func GetInventoryInfoList(info request.InventorySearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.BM_DB.Model(&model.Inventory{})
    var inventorys []model.Inventory
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&inventorys).Error
	return err, inventorys, total
}